package com.asdc.autoscanfiles.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.asdc.autoscanfiles.entity.ImportLog;
import com.asdc.autoscanfiles.service.EsResultService;
import com.asdc.autoscanfiles.service.ImgImportService;
import com.asdc.autoscanfiles.utils.DateUtils;

/**
 * @Description //TODO $ 数据导入
 * @Date $ $
 * @Param $ /import/importFile
 * @return $
 **/
@RestController
@RequestMapping("/api/import")
public class ImgImportController {

	@Resource
	private ImgImportService imgImportService;
	@Autowired
	private EsResultService esResultService;

	@RequestMapping("/logsCount")
	public String getLogCount() {
		return Integer.toString(this.imgImportService.getLogCount());
	}

	@RequestMapping("/logs")
	public String logs(Integer size) {
		if (size == null || size == 0) {
			size = 20;
		}
		List<ImportLog> logs = imgImportService.getRecentLogs(size);
		JSONArray array = new JSONArray();
		if (!CollectionUtils.isEmpty(logs)) {
			for (ImportLog log : logs) {
				array.add(JSONObject.toJSON(log));
			}
		}
		return array.toString();
	}

	@RequestMapping("/importFile")
	public String importFile() {
		imgImportService.importFileAsync();
		return DateUtils.getDateStr(new Date()) + " 导入开始";
	}

}
